package com.tsp_tux.demo.Controller;


import com.tsp_tux.demo.Dao.*;
import com.tsp_tux.demo.Entity.*;
import com.tsp_tux.demo.Service.UserService;
import com.tsp_tux.demo.Util.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;


/**
 * @author 张志豪 沈育洋
 */
@RestController
@CrossOrigin
public class UserController {

    final
    UserRepository userRepository;

    @Resource
    UserService userService;

    public UserController(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    /**
     *@author 周志伟
     * @param uid 用户id
     * @return 博客头
     */
    @GetMapping("/head")
    @ResponseBody
    public RespBean getHead(String uid){
        List<UserHead_List> userHead_lists = userService.getHead(uid);
        if (userHead_lists.isEmpty())
        {
            return new RespBean(404, "Not Found!",null);
        }
        return new RespBean(200, "query Success!",userHead_lists);

    }

    /**
     * @author 张志豪
     * 上传头像
     *
     * @param uid 用户id
     * @param iconFile 图像文件
     * @return 图片外链
     */
    @PostMapping("/changeIcon")
    @CrossOrigin
    public RespBean changeIcon(@RequestParam("uid")String uid,
                               @RequestParam("file") MultipartFile iconFile) {
        return userService.changeIcon(uid, iconFile);
    }

    /**
     * 发送邮箱验证码
     * @author 张志豪
     *
     * @param request 请求
     * @param uid uid
     * @param email 新邮箱
     * @return 处理的信息
     */
    @PostMapping("/changeEmailAddress")
    @CrossOrigin
    public RespBean sendVerifyCode(HttpServletRequest request,
                                 @RequestParam("uid")String uid,
                                 @RequestParam("email")String email) {
        String verifyCode =SendEmail.getVerifyCode();
        userService.sendChangeEmailVerifyCode(uid,email,verifyCode);
        try {
            HttpSession session = request.getSession();
//            HttpSession session = request.getSession(false);
//            if (session == null) {
//                //创建session，并拿到JSESSIONID
//                session = request.getSession();
//            }
            //设置session有效时间，默认是1800s
            session.setMaxInactiveInterval(1800);
            session.setAttribute("verifyCode", verifyCode);
            LocalDateTime dateTime = LocalDateTime.now();
            String time = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
            session.setAttribute("sendTime",time);
            session.setAttribute("uid",uid);
            session.setAttribute("newEmail",email);
            return new RespBean(200, "验证码已发送到邮箱，请前往查收(10分钟内有效)", null);
        }catch (Exception e){
            System.out.println("创建session发生错误");
            return new RespBean(500,"发送错误",null);
        }
    }

    /**
     * 验证验证码是否正确
     * @author 张志豪
     *
     * @param uid uid
     * @param inputCode 输入的验证码
     * @param request 请求
     * @return 修改信息
     */
    @RequestMapping("/verifyChangeEmailCode")
    @CrossOrigin
    public RespBean verifyChangeEmailCode(@RequestParam("uid")String uid,
                           @RequestParam("verifyCode")String inputCode,
                           HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if (session==null){
            return easyRespBean.errWithMsg("验证码已过期，请重新获取");
        }
        if (!uid.equals(session.getAttribute("uid"))){
            return easyRespBean.errWithMsg("请使用正确的账号修改");
        }

        int result=userService.checkVerifyCode(inputCode,session);
        if (result==1){
            return easyRespBean.errWithMsg("验证码已过期，请重新获取");
        }else if (result==2){
            return easyRespBean.errWithMsg("验证码不正确");
        }else if (result==0) {
            String email = (String) session.getAttribute("newEmail");
            //主动使session无效
            session.invalidate();
            return userService.changeEmail(uid,email);
        }else {
            return easyRespBean.errWithMsg("发生错误");
        }
    }

    /**
     * 临时对接用
     */
    @PostMapping("/updateEmail")
    @CrossOrigin
    public RespBean sendVerifyCode(@RequestParam("uid")String uid,
                                   @RequestParam("email")String email){
            return userService.changeEmail(uid, email);
    }

}
